const app = getApp()
Page({
  data: {
    icon:'',  // 头像
    username:'',//昵称
    mobile:'',//手机号
    sex:'',//性别
    birthday:'',//出生日期
    address:'', //家庭住址
    notes:'',//个人留言
    inputValue1: '', // 输入的文本内容
    inputCount1: 0, // 输入的字符数量
    inputValue2: '', // 输入的文本内容
    inputCount2: 0, // 输入的字符数量
   
    currentDate: new Date().getTime(),
    minDate:new Date(1990, 10, 1).getTime(),
    show: false,
    showMobile:false,
    showAdress:false,
    dateLayerVisible:false,
    columns: ['男','女'],
  },
   onLoad() {
        // 没有登录跳出！
        if (!app.token) return
        // 登录了，有用户资料，设置; 没有获取用户资料
        if (app.userInfo){
          this.setData({...app.userInfo})
        } else {
          this.getUserInfo()
        }
      },
  async getUserInfo(){
   const {data:{userInfo:{icon,username,mobile,sex,birthday,address,notes}}} =  await wx.http.get('/user/getUserInfo')
    this.setData({icon,username,mobile,sex,birthday,address,notes})
    app.userInfo = {icon,username,mobile,sex,birthday,address,notes}
  },
  // 用户姓名
  getUsername(ev){
    console.log(ev);
    this.setData({username:ev.detail.value})
    app.userInfo.username = ev.detail.value
  },  
  // 家庭住址
  address(event){
    console.log(event);
    const value = event.detail;
    const count = value.length;
    this.setData({
      inputValue1: value,
      inputCount1: count,
      address:event.detail
    })
    app.userInfo.address = event.detail
  },
  // 个性留言
  mySay(event) {
    console.log(event);
    const value = event.detail;
    const count = value.length;

    this.setData({
      inputValue2: value,
      inputCount2: count,
      notes:event.detail
    })
    app.userInfo.notes = event.detail
    console.log(app.userInfo.notes);
  },
  // 头像
  getUserAvatar(ev){
    this.setData({icon: ev.detail.avatarUrl})
    app.userInfo.icon = ev.detail.avatarUrl
    console.log(ev.detail.avatarUrl);
  },
// 手机号
showMobile() {
  this.setData({ showMobile: true });
},
onCloseMobile(){
  this.setData({ showMobile: false });
},
onClickLeft(){
  this.setData({ showMobile: false });
},
onClickRight(){
  this.setData({ showMobile: false });
},
mobile(ev){
  console.log(ev);
  this.setData({
    mobile:ev.detail
  })
  app.userInfo.mobile = ev.detail
},
// 家庭住址
showAdress() {
  this.setData({ showAdress: true });
},
onCloseAdress(){
  this.setData({ showAdress: false });
},
onClickAdressLeft(){
  this.setData({ showAdress: false });
},
onClickAdressRight(){
  this.setData({ showAdress: false });
},
// 个性留言
showMySay() {
  this.setData({ showMySay: true });
},
onCloseMySay(){
  this.setData({ showMySay: false });
},
onClickMySayLeft(){
  this.setData({ showMySay: false });
},
onClickMySayRight(){
  this.setData({ showMySay: false });
},
   // 性别
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  onCancel(){
    this.setData({ show: false });
  },
  onConfirm(ev){
    this.setData({ show: false });
    this.setData({sex:ev.detail.value})
    app.userInfo.sex = ev.detail.value
  },
//  出生日期
openDateLayer() {
  this.setData({ dateLayerVisible: true })
},
closeDateLayer() {
  this.setData({ dateLayerVisible: false })
},
selectDateInfo(ev){
  this.setData({ dateLayerVisible: false })
      const dateString = ev.detail;
      const date = new Date(dateString);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2,'0');
      const day = String(date.getDate()).padStart(2,'0');
      const formattedDate = `${year}-${month}-${day}`;
    this.setData({birthday:formattedDate})
    app.userInfo.birthday = formattedDate
}
});